//
//  ViewController.m
//  kxmenu
//
//  Created by Kolyvan on 17.05.13.
//  Copyright (c) 2013 Konstantin Bukreev. All rights reserved.
//

#import "ViewController.h"
#import "KxMenu.h"
#import "WEPopoverController.h"

#import "PopoverContentViewController.h"

@interface ViewController ()<WEPopoverControllerDelegate>
{
    
    UIButton *_btn1;
    UIButton *_btn2;
    UIButton *_btn3;
    UIButton *_btn4;
    UIButton *_btn5;
    UIButton *_btn6;
    UIButton *_btn7;
}


@property (nonatomic ,strong)NSMutableArray *items;

@end


@implementation ViewController

- (id)init
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
    }
    return self;
}

- (void) loadView
{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    
    self.view = [[UIView alloc] initWithFrame:frame];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    
    const CGFloat W = self.view.bounds.size.width;
    const CGFloat H = self.view.bounds.size.height;
    
    _btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn1.frame = CGRectMake(5, 5, 100, 50);
    [_btn1 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn1 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn1];
    
    _btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn2.frame = CGRectMake(5, H - 55, 100, 50);
    [_btn2 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn2 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn2];
    
    _btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn3.frame = CGRectMake(W - 105, 5, 100, 50);
    [_btn3 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn3 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn3];
    
    _btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn4.frame = CGRectMake(W - 105, H - 55, 100, 50);
    [_btn4 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn4 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn4];
    
    _btn5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn5.frame = CGRectMake(5, (H-50) * 0.5, 100, 50);
    [_btn5 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn5 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn5];
    
    _btn6 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn6.frame = CGRectMake(W - 105, (H-50) * 0.5, 100, 50);
    [_btn6 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn6 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn6];
    
    _btn7 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btn7.frame = CGRectMake((W - 100)* 0.5, (H-50) * 0.5, 100, 50);
    [_btn7 setTitle:@"Click me" forState:UIControlStateNormal];
    [_btn7 addTarget:self action:@selector(showMenu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn7];

    //[KxMenu setTintColor: [UIColor colorWithRed:15/255.0f green:97/255.0f blue:33/255.0f alpha:1.0]];
    //[KxMenu setTitleFont:[UIFont systemFontOfSize:14]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
    // 导航栏左右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showMenu:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showMenu:)];
 
    
    self.items = [NSMutableArray array];
   
}

- (void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    const CGFloat W = self.view.bounds.size.width;
    const CGFloat H = self.view.bounds.size.height;
    
    _btn1.frame = CGRectMake(5, 5, 100, 50);
    _btn2.frame = CGRectMake(5, H - 55, 100, 50);
    _btn3.frame = CGRectMake(W - 105, 5, 100, 50);
    _btn4.frame = CGRectMake(W - 105, H - 55, 100, 50);
    _btn5.frame = CGRectMake(5, (H-50) * 0.5, 100, 50);
    _btn6.frame = CGRectMake(W - 105, (H-50) * 0.5, 100, 50);
    _btn7.frame = CGRectMake((W - 100)* 0.5, (H-50) * 0.5, 100, 50);
}


#pragma mark -button监听方法
- (void)showMenu1:(UIButton *)button
{
    NSArray *menuItems =
    @[
      
//      [KxMenuItem menuItem:@"ACTION MENU"
//                     image:nil
//                    target:nil
//                    action:NULL],
      
      [KxMenuItem menuItem:@"Share this"
                     image:[UIImage imageNamed:@"action_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Check menu"
                     image:[UIImage imageNamed:@"check_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Reload page"
                     image:[UIImage imageNamed:@"reload"]
                    target:self
                    action:@selector(pushMenuItem:)],

      ];
    
    
    self.items = (NSMutableArray *)menuItems;
    
    if (self.apopoverController) {
        
        [self.apopoverController dismissPopoverAnimated:YES];
        self.apopoverController = nil;
        
        
    } else {
        
        // 初始控制器 及其content控制器
        self.apopoverController = [[WEPopoverController alloc] initWithContentViewControllerByItems:self.items];
        
        self.apopoverController.delegate = self;
        
        // 设置列表弹出后，按钮可被监听
        //self.apopoverController.passthroughViews = [NSArray arrayWithObject:self.view];
        
        // 弹出button对应列表
        [self.apopoverController presentPopoverFromRect:button.frame
                                                inView:self.view
                              permittedArrowDirections:UIPopoverArrowDirectionRight|UIPopoverArrowDirectionUp|UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionDown
                                              animated:YES];
    }

}


#pragma mrak -导航栏Item监听方法
- (void)showMenu:(UIButton *)sender
{
    NSArray *menuItems =
    @[
      
//      [KxMenuItem menuItem:@"ACTION MENU"
//                     image:nil
//                    target:nil
//                    action:NULL],
//            
//      [KxMenuItem menuItem:@"Share this"
//                     image:[UIImage imageNamed:@"action_icon"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Check menu"
                     image:[UIImage imageNamed:@"check_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],

      [KxMenuItem menuItem:@"Reload page"
                     image:[UIImage imageNamed:@"reload"]
                    target:self
                    action:@selector(pushMenuItem:)],
      [KxMenuItem menuItem:@"Share this"
                     image:[UIImage imageNamed:@"action_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Check menu"
                     image:[UIImage imageNamed:@"check_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"Reload page"
                     image:[UIImage imageNamed:@"reload"]
                    target:self
                    action:@selector(pushMenuItem:)]

      ];
    
    
    NSMutableDictionary *itemsDic = [NSMutableDictionary dictionaryWithCapacity:5];
    [itemsDic setValue:@"Chec menu" forKey:@"check_icon"];
    [itemsDic setValue:@"Reload page" forKey:@"reload"];
    
    
    self.items = (NSMutableArray *)menuItems;
    
    if (self.apopoverController != nil) {
        [self.apopoverController dismissPopoverAnimated:YES];
        self.apopoverController = nil;
        
    } else {
        // 初始控制器 及其content控制器
        self.apopoverController = [[WEPopoverController alloc] initWithContentViewControllerByItems:self.items];
       
        self.apopoverController.delegate = self;
        
        self.apopoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
        
        // 弹出导航栏Item对应列表
        [self.apopoverController presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender permittedArrowDirections:UIPopoverArrowDirectionUp  animated:YES];
      
         }

}

#pragma mark -列表按钮监听事件
- (void)pushMenuItem:(UIButton *)sender
{
    // 传入NO ，控制器消失  传YES  控制器移除
    [self.apopoverController dismissPopoverAnimated:YES];
    
    NSLog(@"点击按钮：%@可点击",sender);
}

#pragma mark WEPopoverControllerDelegate 代理方法

- (void)popoverControllerDidDismissPopover:(WEPopoverController *)thePopoverController {
    NSLog(@"菜单控制器删除！");
    self.apopoverController = nil;
}

- (BOOL)popoverControllerShouldDismissPopover:(WEPopoverController *)thePopoverController {

    return YES;
}

@end
